package com.itheima.mall.api.sms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.api.ums.SmsFlashPromotionProductRelationApi;
import com.itheima.mall.dto.ListDtoPage;
import com.itheima.mall.dto.SmsFlashPromotionProduct;
import com.itheima.mall.mapper.pms.ProductMapper;
import com.itheima.mall.mapper.sms.SmsFlashPromotionProductRelationMapper;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.pojo.sms.SmsFlashPromotionProductRelation;
import com.itheima.mall.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class SmsFlashPromotionProductRelationApiImpl
        implements SmsFlashPromotionProductRelationApi {
    @Autowired
    SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    ProductMapper productMapper;
    /**
     * 批量添加上商品关联
     * @param relations
     */
    @Override
    public void addRelation(List<SmsFlashPromotionProductRelation> relations) {
        for (SmsFlashPromotionProductRelation relation : relations) {
            relationMapper.insert(relation);
        }
    }

    @Override
    public void deleteRelation(Long id) {
        relationMapper.deleteById(id);
    }

    @Override
    public PageResult getList(Integer flashPromotionId, Integer flashPromotionSessionId, int pageNum, int pageSize) {
        //Page<SmsFlashPromotionProduct> pageR=new Page<>();
        Page<SmsFlashPromotionProductRelation> pageInfo=new Page<>(pageNum,pageSize);


        LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);
        queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,1);

        Page<SmsFlashPromotionProductRelation> page = relationMapper.selectPage(pageInfo, queryWrapper);
        //BeanUtils.copyProperties(pageInfo,pageR,"records");
        List<SmsFlashPromotionProductRelation> records = page.getRecords();
        List<SmsFlashPromotionProduct> promotionProducts=records.stream().map((item) -> {
            SmsFlashPromotionProduct promotionProduct=new SmsFlashPromotionProduct();
            BeanUtils.copyProperties(item,promotionProduct);
            Long productId = item.getProductId();
            Product product = productMapper.selectById(productId);
            promotionProduct.setProduct(product);
            return promotionProduct;
        }).collect(Collectors.toList());
        //pageR.setRecords(promotionProducts);
        PageResult pageResult=new PageResult();
        pageResult.setPageNum(Convert.toInt(pageInfo.getCurrent()));
        pageResult.setPageSize(Convert.toInt(pageInfo.getSize()));
        pageResult.setTotalPage(Convert.toInt(pageInfo.getPages()));
        pageResult.setList(promotionProducts);
        pageResult.setTotal(Convert.toInt(pageInfo.getTotal()));
        return pageResult;
       /* LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper=new LambdaQueryWrapper<>();
        if (flashPromotionId!=null) {
            queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, Convert.toLong(flashPromotionId));
        }
         *//*if(flashPromotionSessionId!=null)   {
             queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,Convert.toLong(flashPromotionSessionId));
         }*//*
        //queryWrapper.groupBy("flash_promotion_session_id")
        List<SmsFlashPromotionProductRelation> relations =relationMapper.selectList(queryWrapper);
        *//**
             *根据relation去查product
             *//*
        List<SmsFlashPromotionProduct> list=new ArrayList<>();
        for (SmsFlashPromotionProductRelation relation : relations) {
            Product product = productMapper.selectById(relation.getProductId());
            SmsFlashPromotionProduct flashPromotionProduct=new SmsFlashPromotionProduct();
            flashPromotionProduct.setId(relation.getId());
            flashPromotionProduct.setFlashPromotionId(relation.getFlashPromotionId());
            flashPromotionProduct.setFlashPromotionSessionId(relation.getFlashPromotionSessionId());
            flashPromotionProduct.setProductId(relation.getProductId());
            flashPromotionProduct.setFlashPromotionPrice(relation.getFlashPromotionPrice());
            flashPromotionProduct.setFlashPromotionCount(relation.getFlashPromotionCount());
            flashPromotionProduct.setFlashPromotionLimit(relation.getFlashPromotionLimit());
            flashPromotionProduct.setSort(relation.getSort());
            flashPromotionProduct.setProduct(product);//封装到dto里
            list.add(flashPromotionProduct);
        }
        Page<ListDtoPage> pageInfo=new Page<>(pageNum,pageSize);
        ListDtoPage dtoPage=new ListDtoPage();
        dtoPage.setList(list);
        dtoPage.setPageNum(pageNum);
        dtoPage.setTotalPage(5);
        dtoPage.setPageSize(pageSize);
        return dtoPage;*/
    }

    @Override
    public void updateRelation(SmsFlashPromotionProductRelation relation1) {
        relationMapper.updateById(relation1);
    }

    @Override
    public SmsFlashPromotionProductRelation getRelation(Integer id) {
        SmsFlashPromotionProductRelation relation = relationMapper.selectById(Convert.toLong(id));
        return relation;
    }
}
